﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {
    /// <summary>
    /// Interaction logic for Timer.xaml
    /// </summary>
    public partial class Timer : UserControl {


        private System.Windows.Threading.DispatcherTimer TimerClock;
        private System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer();


        private bool timeIsOver = false;

        private DateTime endTime;

        public int InitialMinutes { get; set; }

        public Timer() {
            InitializeComponent();
            InitialMinutes = 15;
            soundPlayer.Stream = Properties.Resources.whistles;            
            TimerClock = new System.Windows.Threading.DispatcherTimer();
            TimerClock.Tick += new EventHandler(TimerClock_Tick);         
        }

        public void Start() {
            timeIsOver = false;
            MinTextBlock.Foreground = (Brush) Resources["NormalBrush"];
            SecTextBlock.Foreground = (Brush)Resources["NormalBrush"];
            TwoDots.Foreground = (Brush)Resources["NormalBrush"];
            soundPlayer.Play();
            endTime = DateTime.Now.AddMinutes(InitialMinutes);
            TimerClock.Start();
        }

        public TimeSpan AvailableTime {
            get {
                if (TimerClock.IsEnabled)
                    return endTime.Subtract(DateTime.Now);
                else {
                    DateTime now = DateTime.Now;
                    return now.AddMinutes(InitialMinutes).Subtract(now);
                }
            }
        }

        private void TimerClock_Tick(object sender, EventArgs e) {
            FormatTimerTextBlock();            
        }


        private void FormatTimerTextBlock() {
        
                int sec = AvailableTime.Seconds;
                if (sec < 0 || timeIsOver) {
                    if (!timeIsOver) {
                        soundPlayer.Play();
                        timeIsOver = true;
                    }
                    sec = sec * -1;
                    MinTextBlock.Foreground = (Brush)Resources["OverTimeBrush"];
                    SecTextBlock.Foreground = (Brush)Resources["OverTimeBrush"];
                    TwoDots.Foreground = (Brush)Resources["OverTimeBrush"];
                }
                MinTextBlock.Text = AvailableTime.Minutes.ToString("00");
                SecTextBlock.Text = sec.ToString("00");
        
        }

        public void Reset() {
            timeIsOver = false;
            TimerClock.Stop();
            FormatTimerTextBlock();
        }
        

        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            if (TimerClock.IsEnabled) {
                Reset();
            } else 
                Start();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            TimerClock.Interval = new TimeSpan(0, 0, 1);            
            FormatTimerTextBlock();            
        }
    }
}
